/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mnstudent.global;

import mnstudent.businessLayer.MajorBLL;
import mnstudent.businessLayer.PriorityObjectDetailBLL;
import mnstudent.businessLayer.StateBLL;
import mnstudent.datatranferobject.AcademicYearDTO;
import mnstudent.datatranferobject.FacultyDTO;
import mnstudent.datatranferobject.KindOfSubjectDTO;
import mnstudent.datatranferobject.MajorDTO;
import mnstudent.datatranferobject.PositionDTO;
import mnstudent.datatranferobject.SemesterDTO;
import mnstudent.datatranferobject.StaffDTO;
import mnstudent.datatranferobject.StateDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author lee
 */
public class DataTable extends DefaultTableModel{

    private static ResultSet m_rsData;
    private final static SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");

    public DataTable(TableColumnModel _dtTableCols) {
    }

    public static void addRow(ResultSet rsData, DefaultTableModel dtTableModel) {
        System.out.println(dtTableModel.getColumnCount());
        System.out.println(dtTableModel.getRowCount());
        m_rsData = rsData;
        int _colsCount;
        try {
            _colsCount = m_rsData.getMetaData().getColumnCount();
            while (m_rsData.next()) {
                Vector _row = new Vector(_colsCount);
                for (int i = 1; i <= _colsCount; ++i) {
                    _row.addElement(m_rsData.getObject(i));
                }
                dtTableModel.addRow(_row);
            }
            dtTableModel.fireTableDataChanged();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

    }

    public static void addRowForMajor(ResultSet rsData, ArrayList<FacultyDTO> arrFaclty, DefaultTableModel dtTableModel) {
        m_rsData = rsData;
        int _colsCount;
        try {
            _colsCount = m_rsData.getMetaData().getColumnCount();
            while (m_rsData.next()) {
                Vector _row = new Vector(_colsCount);
                _row.addElement(m_rsData.getObject(1));
                _row.addElement(m_rsData.getObject(2));
                for (FacultyDTO obj : arrFaclty) {
                    if (m_rsData.getObject(3).equals(obj.getFacultyID())) {
                        _row.addElement(obj);
                        break;
                    }
                }
                dtTableModel.addRow(_row);
            }

            dtTableModel.fireTableDataChanged();
        } catch (SQLException ex) {
            Logger.getLogger(DataTable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void addRowForStudent(ResultSet rsData, DefaultTableModel dtTableModel) {
        m_rsData = rsData;
        MajorBLL m_MajorBLL = new MajorBLL();
        PriorityObjectDetailBLL m_PriorityObjectDetailBLL = new PriorityObjectDetailBLL();
        StateBLL m_StateBLL = new StateBLL();
        try {
            while (m_rsData.next()) {
                Vector rowData;
                rowData = new Vector();

                rowData.addElement(m_rsData.getObject(1));//MSSV
                rowData.addElement(m_rsData.getObject(2));//hoten
                rowData.addElement(m_rsData.getObject(3));//gioi tinh
                rowData.addElement(formatDate.format(m_rsData.getDate(4)));//Ngay sinh
                rowData.addElement(m_rsData.getObject(5));//Huyen
                rowData.addElement(m_rsData.getObject(6));//Tinh

                //Nganh
                ResultSet rs = m_MajorBLL.getMajorById(m_rsData.getString(7));
                while (rs.next()) {
                    MajorDTO major = new MajorDTO(rs.getNString(1), rs.getNString(2), rs.getNString(3));
                    rowData.addElement(major);//nganh
                }
                //Doi tuong
                rowData.addElement(m_rsData.getObject(9));//doi tuong
                //Nam nhap hoc
                rowData.addElement(m_rsData.getObject(10));//nam nhap hoc
                //Trang thai
                rs = m_StateBLL.getStateById(m_rsData.getString(11));
                while (rs.next()) {
                    StateDTO state = new StateDTO(rs.getNString(1), rs.getNString(2));
                    rowData.addElement(state);
                }

                dtTableModel.addRow(rowData);
            }

            dtTableModel.fireTableDataChanged();
        } catch (SQLException ex) {
            Logger.getLogger(DataTable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void addRowForSubject(ResultSet rsData, ArrayList<KindOfSubjectDTO> arrKindOfSubject, DefaultTableModel dtTableModel) {
        m_rsData = rsData;
        int _colsCount;
        try {
            _colsCount = m_rsData.getMetaData().getColumnCount();
            while (m_rsData.next()) {
                Vector _row = new Vector(_colsCount);
                _row.addElement(m_rsData.getObject(1));
                _row.addElement(m_rsData.getObject(2));
                _row.addElement(m_rsData.getObject(3));
                for (KindOfSubjectDTO obj : arrKindOfSubject) {
                    if (m_rsData.getObject(4).equals(obj.getKindOfSubjectID())) {
                        _row.addElement(obj);
                        break;
                    }
                }
                if (m_rsData.getObject(5) == null) {
                    _row.addElement(" ");
                } else {
                    _row.addElement(m_rsData.getObject(5));
                }

                if (m_rsData.getObject(6) == null) {
                    _row.addElement(" ");
                } else {
                    _row.addElement(m_rsData.getObject(6));
                }
                
                dtTableModel.addRow(_row);
            }

            dtTableModel.fireTableDataChanged();
        } catch (SQLException ex) {
            Logger.getLogger(DataTable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
     public static void addRowForStaff(ResultSet rsData, ArrayList<PositionDTO> arrPosition, DefaultTableModel dtTableModel){
        m_rsData = rsData;
        int _colsCount;
        try {
            _colsCount = m_rsData.getMetaData().getColumnCount();
            while(m_rsData.next())
            {                           
                Vector _row = new Vector(_colsCount); 
                _row.addElement(m_rsData.getObject(1));
                _row.addElement(m_rsData.getObject(2));
                _row.addElement(m_rsData.getObject(3));
                _row.addElement(formatDate.format(m_rsData.getDate(4)));
                _row.addElement(m_rsData.getObject(5));
                _row.addElement(m_rsData.getObject(6));
               for(PositionDTO obj : arrPosition){
                   if(m_rsData.getObject(7) != null){
                        if(m_rsData.getObject(7).equals(obj.getPositionID().toString()))
                        {   
                            _row.addElement(obj);
                            break;
                        }
                   }
                   else
                      _row.addElement(null); 
                } 
               _row.addElement(m_rsData.getObject(8));
               dtTableModel.addRow(_row);                 
            }
            
            dtTableModel.fireTableDataChanged();
        } catch (SQLException ex) {
            Logger.getLogger(DataTable.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
     
     public static void addRowInfoSemesterYear(ResultSet rsData, ArrayList<SemesterDTO> arrSemester,  ArrayList<AcademicYearDTO> arrYear, DefaultTableModel dtTableModel){
        m_rsData = rsData;
        int _colsCount;
        try {
            _colsCount = m_rsData.getMetaData().getColumnCount();
            while(m_rsData.next())
            {                           
                Vector _row = new Vector(_colsCount); 
                _row.addElement(m_rsData.getObject(1));
                for(SemesterDTO obj : arrSemester){
                   if(m_rsData.getObject(2) != null){
                        if(m_rsData.getObject(2).equals(obj.getSemesterID().toString()))
                        {   
                            _row.addElement(obj);
                            break;
                        }
                   }
                   else
                      _row.addElement(null); 
                } 
                 for(AcademicYearDTO obj : arrYear){
                   if(m_rsData.getObject(3) != null){
                        if(m_rsData.getObject(3).equals(obj.getAcademicYearID().toString()))
                        {   
                            _row.addElement(obj);
                            break;
                        }
                   }
                   else
                      _row.addElement(null); 
                } 
                _row.addElement(formatDate.format(m_rsData.getObject(4)));
                _row.addElement(formatDate.format(m_rsData.getObject(5)));
                _row.addElement(formatDate.format(m_rsData.getDate(6)));
                _row.addElement(formatDate.format(m_rsData.getObject(7)));
                _row.addElement(m_rsData.getObject(8));
               dtTableModel.addRow(_row);                 
            }
            
            dtTableModel.fireTableDataChanged();
        } catch (SQLException ex) {
            Logger.getLogger(DataTable.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
}
